package b3.widget.rank
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.tab.*;
	
	import flash.events.Event;
	import flash.filters.*;
	import flash.text.*;
	
	public class RankingDialog extends TabController
	{
		private var rankList:RankList;
		
		public function RankingDialog()
		{
			var RankingDialogBgr:Class = ResourceHelper.getInstance().getClassByName("RankingDialogBgr");
			var RankingTitleBgr:Class = ResourceHelper.getInstance().getClassByName("RankingTitleBgr");
			var RankingTitleBgrHighlight:Class = ResourceHelper.getInstance().getClassByName("RankingTitleBgr2");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton2");
			var TabPageFirstPage:Class = ResourceHelper.getInstance().getClassByName("TabPageFirstPage2");
			var TabPagePageUp:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp2");
			var TabPagePageDown:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown2");
			var TabPageLastPage:Class = ResourceHelper.getInstance().getClassByName("TabPageLastPage2");
			super(RankingDialogBgr, CloseButton);
			var tab:RankingTab = new RankingTab(0);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0xFBB102, 0xFBB102);
			tab.setPageSelectorPos(135, 290);
			this.addTab(RankingTitleBgr, tab.tabName, tab, RankingTitleBgrHighlight);
			tab = new RankingTab(1);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0xFBB102, 0xFBB102);
			tab.setPageSelectorPos(135, 290);
			this.addTab(RankingTitleBgr, tab.tabName, tab, RankingTitleBgrHighlight);
			tab = new RankingTab(2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0xFBB102, 0xFBB102);
			tab.setPageSelectorPos(135, 290);
			this.addTab(RankingTitleBgr, tab.tabName, tab, RankingTitleBgrHighlight);
			tab = new RankingTab(3);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0xFBB102, 0xFBB102);
			tab.setPageSelectorPos(135, 290);
			this.addTab(RankingTitleBgr, tab.tabName, tab, RankingTitleBgrHighlight);
			this.setTabTitlePos(34, -8, 79); 
			this.setTabPagePos(88, 90);
			this.setCloseButtonPos(190, 417);
			rankList = new RankList();
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onAddedToStage(event:Event):void
		{
			rankList.loadTotalRankList(true);
			rankList.addEventListener(ModelEvent.REFRESH_RANK_LIST, onRefreshRankList);
		}

		private function onRemovedFromStage(event:Event):void
		{
			rankList.removeEventListener(ModelEvent.REFRESH_RANK_LIST, onRefreshRankList);
		}
		
		private function onRefreshRankList(event:Event):void
		{
			for (var i:int = 0; i < rankList.ranks.length; i++)
			{
				var tab:Tab = this.tabs[i];
				tab.removeAll();				
				for (var j:int = 0; j < rankList.ranks[i].length; j++)
				{
					var rankingItem:RankingItem = new RankingItem(rankList.ranks[i][j]);
					tab.addItem(rankingItem, 87, 92);
					if (j < 2)
					{
						tab.addItem(rankingItem, 87, 92);
					}
				}
			} 
		}
		
	}
}